package com.app.sysmodule.dao.impl;


import com.app.sysmodule.dao.BaseDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;

@Transactional
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> implements BaseDao<T> {
	   @Resource
	    private SessionFactory sessionFactory;

	    private Class<T> clazz=null;
	    
	    @SuppressWarnings("unchecked")
	    public BaseDaoImpl(){
	        ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass();
	        this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
	    }
	    public Session getSession() {
	        return sessionFactory.getCurrentSession();
	    }
	    
	    public void setSessionFactory(SessionFactory sessionFactory)
	    {
	        this.sessionFactory = sessionFactory;
	    }
	    public SessionFactory getSessionFactory()
	    {
	        return this.sessionFactory;
	    }
	    public Long save(T entity) {
	        return    (Long)getSession().save(entity);
	    }

	    public void delete(Long id) {
	        // TODO Auto-generated method stub
	        Object object = getById(id);
	        if(object != null){
	            getSession().delete(object);
	        }
	    }

	    public void update(T entity) {
	        // TODO Auto-generated method stub
	        getSession().update(entity);
	    }

	    public T getById(Long id) {
	        // TODO Auto-generated method stub
	        if(id != null){
	            return (T)getSession().get(clazz, id);
	        }else{
	            return null;
	        }
	    }

	    public List<T> getByIds(Long[] ids) {
	        // TODO Auto-generated method stub\
	        if(ids == null || ids.length == 0){
	            return Collections.emptyList();
	        }else{
	            return getSession().createQuery("from "+
	                    clazz.getSimpleName() + " where id in(:ids)").setParameterList("ids",ids).list();
	        }
	    }
	    
	    public List<T> find(String hql , String param)
	    {
	        // 创建查询
	        Query query = getSession()
	            .createQuery(hql);
	        // 为包含占位符的HQL语句设置参数
	        
	            query.setParameter("1",param);
	        
	        return (List<T>)query.list();
	    }

	    public List<T> findAll() {
	        // TODO Auto-generated method stub
	        return getSession().createQuery("from " + clazz.getSimpleName()).list();
	    }

	    public Long totalNum() {
	        // TODO Auto-generated method stub
	        return (Long)getSession().createQuery("select count(*) from " + clazz.getSimpleName()).uniqueResult();
	    }

	    public List<T> getPage(int pageNow, int pageSize) {
	        // TODO Auto-generated method stub
	        return getSession().createQuery("from " + clazz.getSimpleName()).setFirstResult((pageNow - 1) * pageSize).setMaxResults(pageSize).list();
	    }
	    
	    /*@SuppressWarnings("unchecked")
	    public T get(Class<T> entityClazz , String id)
	    {
	        return (T)((SessionFactory) getSession()).getCurrentSession()
	            .get(entityClazz , id);
	    }*/
}
